package stream;

import lombok.*;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Apple {

    private String color;

    private Integer wight;

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWight() > 150;
    }

    //Predicate里面的test方法，在调用这个方法的时候去实现
    public static List<Apple> filterApple(List<Apple> apples, ApplePredicate test) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : apples) {
            if (test.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static void tets111() {
        List<Apple> apples = new ArrayList<>();
        List<String> s = apples.stream().map(Apple::getColor).collect(Collectors.toList());
        System.out.println(s);
    }


    public static void main(String[] args) {
        tets111();
//        List<Apple> apples = new ArrayList<>();
//        apples.add(new Apple("green",200));
//        apples.add(new Apple("blue",150));
//        apples.add(new Apple("green",80));
//        apples.add(new Apple("pink",300));
//        apples.add(new Apple("green",180));
//        apples.add(new Apple("green",111));
//        Apple.filterApple(apples,new ApplePredicate(){
//
//            @Override
//            public boolean com.test(Apple apple) {
//                return false;
//            }
//        });
////        List<Apple> appleList = Apple.filterApple(apples, new AppleColorGreenPredicate());
//        List<Apple> appleList1 = Apple.filterApple(apples, Apple :: isGreenApple);
////        List<Apple> result = Apple.filterApple(appleList,new AppleWeightPredicate());
////        System.out.println(appleList.toString());
//        System.out.println(appleList1.toString());
        /**
         * stream只是把isGreenApple作为代码片段传给test方法返回回来，lambda实现了将代码块作为参数传给predicate的test方法
         */
//        List<Apple> greenApple = apples.stream().filter(Apple :: isGreenApple).collect(Collectors.toList());
//        Apple apple = new Apple();
//        apples.stream().collect(Collectors.toMap(Apple :: getColor , Apple :: getWight));
//        apples.stream().forEach(e ->{
//            e.setColor(apple.getColor());
//        });
//        File[] hiddenFiles = new File(".").listFiles(File::isHidden);
////        System.out.println("绿色的苹果");
//        System.out.println(greenApple.toString());
//        List<Apple> greenAndWightApple = apples.stream().filter(Apple :: isGreenApple).filter(Apple::isHeavyApple).collect(Collectors.toList());
//        System.out.println("绿色且重于150g的苹果");
//        System.out.println(greenAndWightApple.toString());
//        List<Apple> greenApple1 = apples.stream().filter(a -> a.getColor().equals("green")).collect(Collectors.toList());
//        System.out.println("绿色的苹果");
//        System.out.println(greenApple1.toString());
//        List<Apple> greenApple2 = apples.stream().filter(a -> a.getColor().equals("green") && a.getWight()>150).collect(Collectors.toList());
//        System.out.println("绿色且重于150g的苹果");
//        System.out.println(greenApple2.toString());
    }

}
